import axios from 'axios'
import { BASE_URI } from './mapPath'
import Toast from './Toast'
import RootStore from '../mobx'


const instance = axios.create({
  baseURL: BASE_URI
})

// 请求拦截器
instance.interceptors.response.use(function(config){
  Toast.showLoading('请稍等')
  return config
}, function(error){
  return Promise.reject(error)
})

//相应拦截器
instance.interceptors.response.use(function(config){
  Toast.hideLoading()
  return config.data
}, function(error){
  return Promise(error)
})


export default {
  get: instance.get,
  post: instance.post,
  privatePost: (url, data={}, options={}) => {
    const token = RootStore.token
    const headers = options.headers || {}
    return instance.post(url, data, {
      ...options,
      headers: {
      "Authorization": `Bearer ${token}`,
      ...headers
      }
    })
  }
}